package org.example.controller.v1;

import org.example.exception.DefaultApiException;
import org.example.mapper.UserMapper;
import org.example.pojo.User;
import org.example.service.UserService;
import org.example.util.JsonBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("v1")
public class UserController {

    @Autowired
    private UserMapper mapper;

    @Autowired
    private UserService userService;

    @PostMapping("user/login")
    public JsonBody login(@RequestParam("name") String username,
                          @RequestParam("password") String password) throws DefaultApiException {
        User user = new User();
        user.setName(username);
        user.setPassword(password);
        return userService.login(user);
    }

    @PostMapping("user/register")
    public JsonBody register(@RequestParam("name") String username,
                                 @RequestParam("password") String password,
                                 @RequestParam("sex") Boolean sex) throws DefaultApiException {
        User user = new User();
        user.setPassword(password);
        user.setSex(sex);
        user.setName(username);
        return userService.register(user);
    }
}
